package ex.com.jm.food.action;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.CompanyProfitSummary;
import ex.com.jm.food.service.AdminCPSService;
import ex.com.jm.food.utils.AccountDate;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.ExcelUtil;
import ex.com.jm.food.utils.JsonUtils;

public class AdminCPSAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private CompanyProfitSummary cps;
	private AdminCPSService adminCPSService;
	private String comName;
	private String s_date;
	private String e_date;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String order;
	private String sort;
	private Map<String, String> dateMap;
	public CompanyProfitSummary getCps() {
		return cps;
	}
	public void setCps(CompanyProfitSummary cps) {
		this.cps = cps;
	}
	public AdminCPSService getAdminCPSService() {
		return adminCPSService;
	}
	public void setAdminCPSService(AdminCPSService adminCPSService) {
		this.adminCPSService = adminCPSService;
	}
	public String getComName() {
		return comName;
	}
	public void setComName(String comName) {
		this.comName = comName;
	}
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}

   public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
    public String getOrder() {
	 	return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
    public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}  
/**
    * 到达列表页面	
    * @return
    */
	public String cpsList(){
		dateMap=DateUtils.getDate();
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		return "cpsList";
	}
	/**
	 * 获取数据，返回json
	 */
	public void getCPSs(){
		System.out.println("执行");
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		List<CompanyProfitSummary> cps=this.getAdminCPSService().getCPSs(offset, pageRow, comName, s_date, e_date, sort, order);
	    JSONArray jsoncps=JSONArray.fromObject(cps);
	    totalCount=this.getAdminCPSService().cpsCount(comName, s_date, e_date);
	    JSONObject jo=JsonUtils.buildJson(jsoncps, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	} 
	
	public void CPSExcel(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("application/vnd.ms-excel");
		OutputStream out=null;
		try {
			out=response.getOutputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		List<CompanyProfitSummary> cps=this.getAdminCPSService().CPSExcel(comName, s_date, e_date);
		String[] headers=new String[]{"企业ID","企业名称","售出菜品总量","送餐时间","平均评价","企业收益","网站收益","收益率","时间"};
		String[] columns=new String[]{"comId","comName","totalNum","roomDate","avgEval","comTotalProfit","totalProfit","profitRate","sDate"};
		try {
			ExcelUtil.CPSExcel(cps, headers, columns, out);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void CPSReport(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		List<CompanyProfitSummary> cpss=this.getAdminCPSService().CPSReport(comName, s_date, e_date);
		
		List<String> dateList=new ArrayList<String>();
		List<Double> totalProfit=new ArrayList<Double>();
		List<Double> comTotalProfit=new ArrayList<Double>();
		List<Integer> totalNum=new ArrayList<Integer>();
		List<Integer> profitRate=new ArrayList<Integer>();
		for (CompanyProfitSummary cps : cpss) {
			totalProfit.add(cps.getTotalProfit());
			comTotalProfit.add(cps.getComTotalProfit());
			totalNum.add(cps.getTotalNum());
			profitRate.add(cps.getProfitRate());
			dateList.add(cps.getSDate());
		}
		JSONArray jototalprofit=JSONArray.fromObject(totalProfit);
		JSONArray jocomTotalProfit=JSONArray.fromObject(jototalprofit);
		JSONArray jototalNum=JSONArray.fromObject(totalNum);
		JSONArray joprofitRate=JSONArray.fromObject(profitRate);
		JSONArray jodate=JSONArray.fromObject(dateList);
		JSONObject jo=new JSONObject();
		jo.put("totalProfit", jototalprofit);
		jo.put("comTotalProfit", jocomTotalProfit);
		jo.put("totalNum", jototalNum);
		jo.put("profitRate", joprofitRate);
		jo.put("date", jodate);
		System.out.println(jo);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
	
}
